package com.algebra.util.encrypt;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.Configuration;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @author al
 * @date 2022/5/24 17:18
 * @description 新增和更新（敏感字段加密处理拦截器）
 */
@Intercepts({@Signature(
        type = Executor.class,
        method = "update",
        args = {MappedStatement.class, Object.class}
)})
public class InsertDataFieldFilter implements Interceptor {

    private EncryptorProperties encryptorProperties;

    public InsertDataFieldFilter(){}

    public InsertDataFieldFilter(EncryptorProperties encryptorProperties){
        this.encryptorProperties = encryptorProperties;
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] var3 = invocation.getArgs();
        MappedStatement var4 = (MappedStatement) var3[0];
        if (insertOrUpdate(var4.getSqlCommandType())) {
            Object var5 = var3[1];
            Configuration var6 = var4.getConfiguration();
            if (var5 instanceof Map) {
                Iterator var7 = ((Map) var5).entrySet().iterator();
                while (true) {
                    Map.Entry var8;
                    Object var9;
                    label40:
                    do {
                        do {
                            do {
                                if (!var7.hasNext()) {
                                    return invocation.proceed();
                                }
                                var8 = (Map.Entry) var7.next();
                                var9 = var8.getValue();
                            } while (null == var9);

                            if (var9 instanceof ArrayList) {
                                continue label40;
                            }
                        } while (!FieldCrypt.encryptField(var6, encryptorProperties, var9));

                        return invocation.proceed();
                    } while (((String) var8.getKey()).startsWith("param"));

                    Iterator var10 = ((ArrayList) var9).iterator();

                    while (var10.hasNext()) {
                        Object var11 = var10.next();
                        if (FieldCrypt.encryptField(var6, encryptorProperties, var11)) {
                        }
                    }
                }
            } else {
                FieldCrypt.encryptField(var6, encryptorProperties, var5);
            }
        }

        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return target instanceof Executor ? Plugin.wrap(target, this) : target;
    }

    @Override
    public void setProperties(Properties properties) {

    }

    public static boolean insertOrUpdate(SqlCommandType var0) {
        return SqlCommandType.UPDATE == var0 || SqlCommandType.INSERT == var0;
    }
}
